home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Amiga Format CD 42
/
Amiga Format AFCD42 (Issue 126, Aug 1999).iso
/
-serious-
/
comms
/
other
/
slrn
/
slrn_src
/
src
/
interp.c
< prev
next >
Wrap
C/C++ Source or Header
|
1999-05-14
|
25KB
|
1,125 lines
/* -*- mode: C; mode: fold -*- */
/* Copyright (c) 1998 John E. Davis (davis@space.mit.edu)
*
* This file is part of slrn.
*
* Slrn is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2, or (at your option) any
* later version.
*
* Slrn is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*
* You should have received a copy of the GNU General Public License
* along with Slrn; see the file COPYING. If not, write to the Free
* Software Foundation, 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#include "config.h"
#include "slrnfeat.h"
#include <stdio.h>
/* rest of file inside this #if statement */
#if SLRN_HAS_SLANG
/*{{{ Include files */
#if HAVE_STDLIB_H
# include <stdlib.h>
#endif
#include <string.h>
#include <slang.h>
#include "jdmacros.h"
#include "slrn.h"
#include "group.h"
#include "art.h"
#include "misc.h"
#include "startup.h"
#include "server.h"
#include "menu.h"
#include "interp.h"
#include "util.h"
#include "print.h"
/*}}}*/
/*{{{ Public Global Variables */
int Slrn_Use_Slang = 0;
char *Slrn_Macro_Dir;
/*}}}*/
/*{{{ Screen update and message functions */
static void error (char *msg) /*{{{*/
{
slrn_error ("%s", msg);
}
/*}}}*/
static void update (void) /*{{{*/
{
slrn_update_screen ();
}
/*}}}*/
static void free_argv_list (char **argv, unsigned int argc)
{
unsigned int i;
for (i = 0; i < argc; i++)
slrn_free (argv[i]);
slrn_free ((char *) argv);
}
static char **pop_argv_list (unsigned int *argcp)
{
int n;
char **argv;
unsigned int i, argc;
if (-1 == SLang_pop_integer (&n))
return NULL;
if (n < 0)
{
slrn_error ("positive integer expected");
return NULL;
}
argc = (unsigned int) n;
if (NULL == (argv = (char **) slrn_malloc (sizeof (char *) * (argc + 1), 1, 1)))
{
SLang_Error = SL_MALLOC_ERROR;
return NULL;
}
argv [n] = NULL;
i = argc;
while (i != 0)
{
i--;
if (-1 == SLpop_string (argv + i))
{
free_argv_list (argv, argc);
return NULL;
}
}
*argcp = argc;
return argv;
}
static int interp_select_box (void) /*{{{*/
{
unsigned int n;
Slrn_Select_Box_Type box;
int ret;
if (Slrn_Batch)
{
slrn_error ("select box function not available in batch mode.");
return -1;
}
if (NULL == (box.lines = pop_argv_list (&n)))
return -1;
if (-1 == SLpop_string (&box.title))
{
free_argv_list (box.lines, n);
return -1;
}
ret = slrn_select_box (&box);
free_argv_list (box.lines, n);
slrn_free (box.title);
return ret;
}
/*}}}*/
static void select_list_box (void)
{
char *title;
unsigned int argc;
char **argv;
int active;
int ret;
if (-1 == SLang_pop_integer (&active))
return;
argv = pop_argv_list (&argc);
if (argv == NULL)
return;
if (-1 == SLpop_string (&title))
{
free_argv_list (argv, argc);
return;
}
ret = slrn_select_list_mode (title, argc, argv, active - 1, NULL);
if (ret == -1)
{
SLang_push_string ("");
return;
}
SLang_push_string (argv[ret]);
slrn_free (title);
free_argv_list (argv, argc);
}
static int get_yesno_cancel (char *prompt)
{
return slrn_get_yesno_cancel ("%s", prompt);
}
static int get_response (char *choices, char *prompt)
{
return slrn_get_response (choices, "%s", prompt);
}
static void tt_send (char *s)
{
if (Slrn_Batch == 0)
{
SLtt_write_string (s);
SLtt_flush_output ();
}
}
/*}}}*/
/*{{{ File functions */
static void make_home_filename (char *name) /*{{{*/
{
char file [SLRN_MAX_PATH_LEN];
slrn_make_home_filename (name, file);
SLang_push_string (file);
}
/*}}}*/
static int evalfile (char *file)
{
if (-1 == slrn_eval_slang_file (file))
return 0;
return 1;
}
int slrn_eval_slang_file (char *name) /*{{{*/
{
char file [SLRN_MAX_PATH_LEN];
if (Slrn_Macro_Dir != NULL)
{
int n = 0;
char dir[SLRN_MAX_PATH_LEN];
while (1)
{
if (-1 == SLextract_list_element (Slrn_Macro_Dir, n, ',',
file, sizeof (file)))
break;
slrn_make_home_dirname (file, dir);
if ((-1 != slrn_dircat (dir, name, file))
&& (1 == slrn_file_exists (file)))
{
slrn_message_now ("loading %s", file);
if (-1 == SLang_load_file (file))
return -1;
return 0;
}
n++;
}
}
slrn_make_home_filename (name, file);
slrn_message_now ("loading %s", file);
if (0 == SLang_load_file (file))
return -1;
return 0;
}
/*}}}*/
/*}}}*/
/*{{{ Set/Get Variable Functions */
static void set_string_variable (char *s1, char *s2) /*{{{*/
{
if (-1 == slrn_set_string_variable (s1, s2))
slrn_error ("%s is not a valid variable name.", s1);
}
/*}}}*/
static void set_integer_variable (char *s1, int *val) /*{{{*/
{
if (-1 == slrn_set_integer_variable (s1, *val))
slrn_error ("%s is not a valid variable name.", s1);
}
/*}}}*/
static void get_variable_value (char *name) /*{{{*/
{
char **s;
int *ip;
int type;
if (-1 == slrn_get_variable_value (name, &type, &s, &ip))
{
slrn_error ("%s is not a valid variable name.", name);
return;
}
if (type == STRING_TYPE)
{
char *str;
if ((s == NULL) || (*s == NULL)) str = "";
else str = *s;
SLang_push_string (str);
}
else if (type == INT_TYPE)
{
int i;
if (ip == NULL) i = 0; else i = *ip;
SLang_push_integer (i);
}
}
/*}}}*/
/*}}}*/
static char *get_server_name (void) /*{{{*/
{
if ((NULL == Slrn_Server_Obj)
|| (NULL == Slrn_Server_Obj->sv_name))
return "";
return Slrn_Server_Obj->sv_name;
}
/*}}}*/
static void quit (int *code) /*{{{*/
{
slrn_quit (*code);
}
/*}}}*/
/*{{{ Keyboard related functions */
static void definekey (char *fun, char *key, char *map) /*{{{*/
{
SLKeyMap_List_Type *kmap;
if (NULL == (kmap = SLang_find_keymap (map)))
{
error ("definekey: no such keymap.");
return;
}
if (0 != SLang_define_key (key, fun, kmap))
{
}
}
/*}}}*/
static void undefinekey (char *key, char *map) /*{{{*/
{
SLKeyMap_List_Type *kmap;
if (NULL == (kmap = SLang_find_keymap (map)))
{
error ("undefinekey: no such keymap.");
return;
}
SLang_undefine_key (key, kmap);
}
/*}}}*/
static void generic_read_mini (int no_echo, char *prompt, char *dfl, char *init) /*{{{*/
{
char str[256];
int ret;
strncpy (str, init, sizeof (str));
str[sizeof(str) - 1] = 0;
if (no_echo)
ret = slrn_read_input_no_echo (prompt, dfl, str, 0, 0);
else
ret = slrn_read_input (prompt, dfl, str, 0, 0);
if (-1 == ret)
{
error ("Quit!");
return;
}
SLang_push_string (str);
}
/*}}}*/
static void read_mini (char *prompt, char *dfl, char *init) /*{{{*/
{
generic_read_mini (0, prompt, dfl, init);
}
/*}}}*/
static void read_mini_no_echo (char *prompt, char *dfl, char *init) /*{{{*/
{
generic_read_mini (1, prompt, dfl, init);
}
/*}}}*/
static void set_prefix_arg (int *arg) /*{{{*/
{
slrn_set_prefix_argument (*arg);
}
/*}}}*/
static int get_prefix_arg (void) /*{{{*/
{
#define NO_PREFIX_ARGUMENT -1
return Slrn_Prefix_Arg_Ptr ? *Slrn_Prefix_Arg_Ptr : NO_PREFIX_ARGUMENT;
}
/*}}}*/
static void reset_prefix_arg (void) /*{{{*/
{
Slrn_Prefix_Arg_Ptr = NULL;
}
/*}}}*/
static int check_tty (void)
{
if (Slrn_TT_Initialized & SLRN_TTY_INIT)
return 0;
error ("Terminal not initialized.");
return -1;
}
static int input_pending (int *tsecs)
{
if (check_tty ())
return 0;
return SLang_input_pending (*tsecs);
}
static int getkey (void)
{
if (check_tty ())
return 0;
return SLang_getkey ();
}
static void ungetkey (int *c)
{
if (check_tty ())
return;
SLang_ungetkey (*c);
}
static void set_input_string (